<?php
/**
 * Created by PhpStorm.
 * User: 程阳
 * Date: 2017/2/22
 * Time: 0:59
 */

namespace Admin\Controller;


use Common\Controller\AdminController;
use Org\Util\Data;

class ClassController extends AdminController
{
    // 分类列表
    public function lists(){
        $listsData = D('Class')->select();
        $listsData = (new Data())->tree($listsData,'cname');
        $this->assign('listsData',$listsData);
        $typeData = M('Type')->select();
        $this->assign('typeData',$typeData);
        $this->display();
    }

    // 添加顶级分类
    public function addClass(){
        if(IS_POST){
            $data = I('post.');
            $this->store(D("Class"),$data,function(){
                $this->success('分类提交成功',u('admin/class/lists'));die;
            });
        }
        $typeData = D("Type")->select();
        $this->assign('typeData',$typeData);
        $this->display();
    }

    // 删除分类
    public function delClass(){
        $cid = I('post.id');
        $pid = M("Class")->where("cid='{$cid}'")->getField('pid');
        $upSon = M("Class")->where("pid='{$cid}'")->save(['pid'=>$pid]);
        $res = D('Class')->delete($cid);
        if($res || $upSon){
            return show(1,'删除成功');
        }else{
            return show(0,'删除失败');
        }
    }

    // 添加子分类
    public function addSonclass(){
        $cid = I('get.cid');
        $classData = D("Class")->where("cid='{$cid}'")->find();
        $this->assign('classData',$classData);
        if(IS_POST){
            $data = I('post.');
            $this->store(D("Class"),$data,function(){
                $this->success('分类提交成功',u('admin/class/lists'));die;
            });
        }
        $typeData = D("Type")->where("tid='" . $classData['tid'] . "'")->find();
        $this->assign('typeData',$typeData);
        $this->display();
    }

    // 分类编辑
    public function edit(){
        $cid = I('get.cid');
        $classData = D("Class")->where("cid='{$cid}'")->find();

        $this->assign('classData',$classData);
        $classDatas = D("Class")->select();

        $cids = $this->getSon($classDatas,$cid);
        $cids[] = $cid;
        $map['cid']  = array('not in',$cids);
        $classDatas = D("Class")->where($map)->select();
        $classDatas = (new Data())->tree($classDatas,'cname');
        $this->assign('classDatas',$classDatas);
        if(IS_POST){
            $data = I('post.');
            if($cid){
                $data['cid'] = $cid;
            }
            $tid = $data['tid'];
            M("Class")->where("pid={$cid}")->save(["tid"=>$tid]);
            $this->store(D("Class"),$data,function(){
                $this->success('分类提交成功',u('admin/class/lists'));die;
            });
        }
        $typeData = D("Type")->where("tid='" . $classData['tid'] . "'")->find();
        $this->assign('typeData',$typeData);
        $typeDatas = D("Type")->select();
        $this->assign('typeDatas',$typeDatas);
        $this->display();
    }

    // 获取所有子分类id
    public function getSon($data,$cid){
        static $cids = [];
        foreach($data as $k => $v){
            if($v['pid'] == $cid){
                $cids[] = $v['cid'];
                $this->getSon($data,$v['cid']);
            }
        }
        return $cids;
    }

    // 修改父级分类后返回父级分类所对应的类型id
    public function change(){
        $cid = I('post.cid');
        $typeChange = D("Class")->where("cid='" . $cid . "'")->find();
        echo json_encode($typeChange,JSON_UNESCAPED_UNICODE);
    }
}